package com.zcb.rxjavapractice;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zcb.rxjavapractice.bean.Yy;
import com.zcb.rxjavapractice.api.ApiService;
import com.zcb.rxjavapractice.api.HttpResultSubscriber;
import com.zcb.rxjavapractice.api.ServiceFactory;
import com.zcb.rxjavapractice.api.service.CloudccService;
import com.zcb.rxjavapractice.api.service.impl.CloudccServiceImpl;
import com.zcb.rxjavapractice.view.DemoView;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class Demo1Activity extends AppCompatActivity implements View.OnClickListener,DemoView{

    CloudccService cloudccService=new CloudccServiceImpl(this);

    private Button reqBtn;
    private Button btnSendValidNum;
    private Button btn_query_yy_info;
    private TextView showTV;
    private LinearLayout linearLayout;
    private ListView listView;
    private ArrayAdapter adapter;

    private List list;

    public void setCloudccService(CloudccServiceImpl cloudccService) {
        this.cloudccService = cloudccService;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_demo1);
        reqBtn= (Button) findViewById(R.id.btn_request);
        showTV= (TextView) findViewById(R.id.tv_response);
        btnSendValidNum= (Button) findViewById(R.id.btn_send_validnum);
        btn_query_yy_info= (Button) findViewById(R.id.btn_query_yy_info);
        btn_query_yy_info.setOnClickListener(this);
        listView= (ListView) findViewById(R.id.ls_res);
        list=new ArrayList();
        adapter=new ArrayAdapter<Yy>(this,R.layout.simple,list);
        listView.setAdapter(adapter);
        btnSendValidNum.setOnClickListener(this);
        reqBtn.setOnClickListener(this);


    }

    private void getresponse(){
        Observable observable=ServiceFactory.getInstance().createService(ApiService.class).allHospitals();
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpResultSubscriber<Yy>() {

                    @Override
                    public void onSuccess(Yy yy) {
                        TextView tv=new TextView(Demo1Activity.this);
                        tv.setText(yy.name);
//                        showTV.setText(getIpInfoResponse.result);

                    }
                    @Override
                    public void _onError(Throwable e) {

                    }
                });
    }
    private void queryYyInfo(){
//        JSONObject json = new JSONObject();
//        try {
//            json.put("firstName", "zhang");
//            json.put("lastName", "zb");
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        Map<String, String> map = new HashMap<String, String>();
//        map.put("name", json.toString());
//        map.put("age", "18");
//        map.put("sex", "man");
//        Observable observable=ServiceFactory.getInstance().createService(ApiService.class).querycommon(map);
//            cloudccService.queryCommon("yy","id='a3120168962A5CBtLm28'","false");
        cloudccService.queryCommon("yy","","false");

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_request:
                getresponse();
            break;
            case R.id.btn_send_validnum:
                sendValidNum();
                break;
            case R.id.btn_query_yy_info:
                queryYyInfo();
                break;
        }
    }

    /**
     * 发送验证码
     */
    private void sendValidNum(){
        if (cloudccService!=null){
        cloudccService.sendVlidNum("13405995634");
        }else{

            Toast.makeText(this,"空对象",Toast.LENGTH_SHORT).show();
        }

    }


    @Override
    public void refreshList(List list) {
        showTV.setText("请求到"+list.size()+"行数据");
    }
}
